package com.zkthink.ceres.product.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zkthink.base.R;
import com.zkthink.base.request.PageParams;
import com.zkthink.ceres.common.enums.YesOrNoEnum;
import com.zkthink.ceres.product.dao.ProductImgMapper;
import com.zkthink.ceres.product.dao.ProductMapper;
import com.zkthink.ceres.product.dao.SubjectProductMapper;
import com.zkthink.ceres.product.dto.StoreDTO;
import com.zkthink.ceres.product.dto.SubjectPageDTO;
import com.zkthink.ceres.product.entity.Product;
import com.zkthink.ceres.product.entity.ProductImg;
import com.zkthink.ceres.product.entity.SubjectProduct;
import com.zkthink.ceres.product.service.SubjectProductService;
import com.zkthink.base.service.SuperServiceImpl;

import com.zkthink.ceres.product.vo.ProductPageVO;
import com.zkthink.ceres.product.vo.ProductVO;
import com.zkthink.ceres.store.entity.Store;
import com.zkthink.ceres.store.service.StoreService;
import com.zkthink.database.mybatis.conditions.Wraps;
import com.zkthink.dozer.DozerUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 专题商品表
 * </p>
 *
 * @author JustArgo
 * @date 2020-09-11
 */
@Slf4j
@Service
@AllArgsConstructor
public class SubjectProductServiceImpl extends SuperServiceImpl<SubjectProductMapper, SubjectProduct> implements SubjectProductService {

    private ProductMapper productMapper;

    private DozerUtils dozerUtils;

    private ProductImgMapper productImgMapper;

    private StoreService storeService;

    @Override
    public R<ProductPageVO> querySubjectDetail(PageParams<SubjectProduct> pageParams) {
        IPage<SubjectProduct> itemPage = baseMapper.selectPage(pageParams.getPage(), Wraps.<SubjectProduct>lbQ().eq(SubjectProduct::getSubjectId, pageParams.getModel().getSubjectId())
                .eq(SubjectProduct::getDisplay, YesOrNoEnum.YES.getCode())
                .eq(SubjectProduct::getIsDelete, YesOrNoEnum.NO.getCode()));
        ProductPageVO pageVO = new ProductPageVO();
        if (itemPage != null && CollectionUtils.isNotEmpty(itemPage.getRecords())) {
            List<Product> productList = productMapper.selectBatchIds(itemPage.getRecords().stream().map(item -> item.getProductId()).collect(Collectors.toList()));

            List<ProductVO> productVOList = dozerUtils.mapList(productList, ProductVO.class);
            for (ProductVO productVO : productVOList) {
                List<ProductImg> productImgs = productImgMapper.selectByProductId(productVO.getId());
                List<String> imgs = new ArrayList<String>();
                for (ProductImg productImg : productImgs) {
                    imgs.add(productImg.getImgPath());
                }
                productVO.setImgs(imgs);

                //查询付款人数
                Integer buyCount = productMapper.getProductBuyCount(productVO.getId());
                productVO.setBuyCount(buyCount);
                Store store = storeService.getById(productVO.getStoreId());
                productVO.setStoreDTO(dozerUtils.map(store, StoreDTO.class));
            }

            pageVO.setTotal((int) itemPage.getTotal());
            pageVO.setProductVOList(productVOList);
            return R.success(pageVO);
        }

        pageVO.setTotal(0);
        pageVO.setProductVOList(new ArrayList<>());
        return R.success(pageVO);
    }
}
